package live.common;

import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemCached {
	protected static MemCachedClient mcc = new MemCachedClient();
	protected static MemCached memcached = null;
	
	static{
		String[] servers={"127.0.0.1:3313"};
		Integer[] weights={3};
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(servers);
		pool.setWeights(weights);
		pool.setInitConn(5);
		pool.setMinConn(5);
		pool.setMaxConn(500);
		pool.setMaxIdle(1000*60*60*6);
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setSocketConnectTO(0);
		pool.initialize();
	}
	
	public MemCached() {
	}
	
	public static MemCached getInstance(){
		if(memcached==null){
			memcached=new MemCached();
		}
		return memcached;
	}
	
		/**  
	     * 添加一个指定的值到缓存中.  
	     * @param key  
	     * @param value  
	     * @return  
	     */  
	    public boolean add(String key, Object value)   
	    {   
	        return mcc.add(key, value);   
	    }   
	     
	    /**
	     * 添加一个指定的值到缓存中，并设置有效期
	     * @param key
	     * @param value
	     * @param expiry
	     * @return
	     */
	    public boolean add(String key, Object value, Date expiry)   
	    {   
	        return mcc.add(key, value, expiry);   
	    }   
	     /**
	      * 修改缓存中一个指定key的值
	      * @param key
	      * @param value
	      * @return
	      */
	    public boolean replace(String key, Object value)   
	    {   
	        return mcc.replace(key, value);   
	    }   
	    
	    /**
	     * 修改缓存中一个指定key的值，并设置有效期
	     * @param key
	     * @param value
	     * @param expiry
	     * @return
	     */
	    public boolean replace(String key, Object value, Date expiry)   
	    {   
	        return mcc.replace(key, value, expiry);   
	    }   
	      
	    /**  
	     * 根据指定的关键字获取对象.  
	     * @param key  
	     * @return  
	     */  
	    public Object get(String key)   
	    {   
	        return mcc.get(key);   
	    } 
	    
	    public Object delete(String key)   
	    {   
	        return mcc.delete(key);   
	    } 
}
